package com.phoenix.service.impl;

import com.phoenix.entity.*;
import com.phoenix.mapper.*;
import com.phoenix.service.SignsService;
import com.phoenix.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class SignsServiceImpl implements SignsService {
    @Autowired
    EventsMapper eventsMapper;
    @Autowired
    MemberEventsMapper memberEventsMapper;
    @Autowired
    GroupEventsMapper groupEventsMapper;
    @Autowired
    UserMemberMapper userMemberMapper;
    @Autowired
    GroupMemberMapper groupMemberMapper;

    @Override
    public List<Events> selectNSearch(Map<String, Object> search, Integer userID) {
        String type = (String) search.get("type");
        List<Events> list = eventsMapper.selectNSearch(search);
        List<Integer> memberEventslist = List.of();
        List<Integer> groupEventslist = List.of();
        List<Integer> userMemberList;
        List<Integer> groupMemberList;
        if (!list.isEmpty()) {
            if (type.equals("2")) {
                userMemberList = userMemberMapper.selectMemberIDByUserId(userID);
                if (!userMemberList.isEmpty()) {
                    memberEventslist = memberEventsMapper.selectSignsByID(userMemberList);
                }
                for (Events events : list) {
                    if (!userMemberList.isEmpty()) {
                        if (!memberEventslist.isEmpty()) {
                            if (memberEventslist.stream().anyMatch(a -> a.equals(events.getEventsID()))) {
                                events.setSignStatus("1");
                            } else events.setSignStatus("0");
                        } else events.setSignStatus("0");
                    } else events.setSignStatus("0");
                }

            } else {
                groupMemberList = groupMemberMapper.selectMemberIDByUserId(userID);
                if (!groupMemberList.isEmpty()) {
                    groupEventslist = groupEventsMapper.selectSignsByID(groupMemberList);
                }
                for (Events events : list) {
                    if (!groupMemberList.isEmpty()) {
                        if (!groupEventslist.isEmpty()) {
                            if (groupEventslist.stream().anyMatch(a -> a.equals(events.getEventsID()))) {
                                events.setSignStatus("1");
                            } else events.setSignStatus("0");
                        } else events.setSignStatus("0");
                    } else events.setSignStatus("0");
                }
            }
        }
        return list;
    }

    @Override
    public Boolean signEvents(Integer eventsID, Integer userID, String type) {
        if (type.equals("团体会员活动报名")) {
            List<Integer> groupMemberList = groupMemberMapper.selectMemberIDByUserId(userID);
            if (!groupMemberList.isEmpty()) {
                for (Integer i : groupMemberList) {
                    groupEventsMapper.groupSign(eventsID, i);
                }
                return true;
            } else return false;

        } else {
            List<Integer> userMemberList = userMemberMapper.selectMemberIDByUserId(userID);
            if (!userMemberList.isEmpty()) {
                for (Integer i : userMemberList) {
                    memberEventsMapper.memberSign(eventsID, i);
                }
                return true;
            } else return false;
        }
    }

    @Override
    public Boolean unSignEvents(Integer eventsID, Integer userID, String type) {
        if (type.equals("团体会员活动报名")) {
            List<Integer> groupMemberList = groupMemberMapper.selectMemberIDByUserId(userID);
            if (!groupMemberList.isEmpty()) {
                for (Integer i : groupMemberList) {
                    groupEventsMapper.groupUnSign(eventsID, i);
                }
                return true;
            } else return false;

        } else {
            List<Integer> userMemberList = userMemberMapper.selectMemberIDByUserId(userID);
            if (!userMemberList.isEmpty()) {
                for (Integer i : userMemberList) {
                    memberEventsMapper.memberUnSign(eventsID, i);
                }
                return true;
            } else return false;
        }
    }

    @Override
    public EventsSigns eventsSigns(Integer eventsID) {
        List<Integer> memberSignsEventslist = memberEventsMapper.selectMemberEventsByID(eventsID);
        List<Integer> groupSignsEventslist = groupEventsMapper.selectGroupEventsByID(eventsID);
        String tempMemberName;
        String tempGroupName;
        EventsSigns eventsSigns = new EventsSigns();
        if (!memberSignsEventslist.isEmpty()) {
            for (Integer i : memberSignsEventslist) {
                tempMemberName = userMemberMapper.selectUserNameByInfoID(i);
                eventsSigns.setObjectsName(eventsSigns.getObjectsName() + "," + tempMemberName);
            }
        }
        if (!groupSignsEventslist.isEmpty()) {
            for (Integer i : groupSignsEventslist) {
                tempGroupName = groupMemberMapper.selectOrgNameByInfoID(i);
                eventsSigns.setObjectsName(eventsSigns.getObjectsName() + "," + tempGroupName);
            }
        }
        return eventsSigns;
    }
}
